package cn.meye11.crm.controller;

import cn.meye11.crm.common.RestResponse;
import cn.meye11.crm.domain.model.Member;
import cn.meye11.crm.domain.query.MemberQuery;
import cn.meye11.crm.service.MemberService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员信息表(Member)表服务控制类
 *
 * @author makejava
 * @since 2023-11-11 13:34:01
 */
@Slf4j
@Controller
@RequestMapping("member")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MemberController {

    private final MemberService memberService;

    // 代码写到此区域
    @PostMapping("refund/{id}")
    @ResponseBody
    public RestResponse<String> refund(@PathVariable Long id) {
        try {
            memberService.refund(id);
            return RestResponse.success("退款完成!");
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @GetMapping("manage")
    public String manage(Model model, HttpSession session) {
        model.addAttribute("user", session.getAttribute("user"));
        return "member/member_list";
    }


    /************************************* 请在上方编写新代码 **************************************/
    // 此区域为自动生成的通用代码 尽量不直接修改
    @PostMapping(value = "save")
    @ResponseBody
    public RestResponse<Long> save(@RequestBody Member model) {
        try {
            Long id = memberService.save(model);
            return RestResponse.success("成功", id);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }


    @DeleteMapping(value = "remove/{id}")
    @ResponseBody
    public RestResponse<String> remove(@PathVariable Long id) {
        try {
            memberService.remove(id);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
        return RestResponse.success("成功");
    }

    @PostMapping(value = "update")
    @ResponseBody
    public RestResponse<String> update(@RequestBody Member model) {
        if (model.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        try {
            memberService.update(model);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
        return RestResponse.success("成功");
    }

    @GetMapping(value = "get")
    @ResponseBody
    public RestResponse<Member> get(Long id) {
        Member model = memberService.get(id);
        return model == null ? RestResponse.failed("不存在") : RestResponse.success("成功", model);
    }

    @GetMapping(value = "count")
    @ResponseBody
    public RestResponse<Long> count(MemberQuery query) {
        Long count = memberService.count(query);
        return RestResponse.success("成功", count);
    }

    @GetMapping(value = "list")
    @ResponseBody
    public RestResponse<List<Member>> list(MemberQuery query) {
        return memberService.list(query);
    }

    @GetMapping(value = "page")
    @ResponseBody
    public RestResponse<List<Member>> page(MemberQuery query) {
        return memberService.page(query);
    }
}
